package com.firefly.mq.redisMq.container;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.firefly.dao.util.RedisUtils;
import com.firefly.mq.redisMq.job.Job;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @description: job任务池，为普通的K/V结构，提供基础的操作
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/10/28
 **/
@Component
@Slf4j
public class JobPool {

    @Autowired
    private RedisUtils redisUtils;

    private String NAME = "job.pool";

    public Map getPool () {
        Map ops = redisUtils.hmget(NAME);
        return ops;
    }

    /**
     * 添加任务
     * @param job
     */
    public void addJob (Job job) throws JsonProcessingException {
//        log.info("任务池添加任务：{}", JsonUtil.obj2Str(job));
        redisUtils.hset(NAME,job.getId().toString(),job);
    }

    /**
     * 获得任务
     * @param jobId
     * @return
     */
    public Job getJob(Long jobId) {
        Object o = redisUtils.hget(NAME,jobId.toString());
        if (o instanceof Job) {
            return (Job) o;
        }
        return null;
    }

    /**
     * 移除任务
     * @param jobId
     */
    public void removeDelayJob (Long jobId) {
//        log.info("任务池移除任务：{}",jobId);
        // 移除任务
        redisUtils.hdel(NAME,jobId.toString());
    }
}
